package com.fulihui.usercore.core.repository.impl;

import com.fulihui.usercore.core.repository.WechatUserRepository;
import com.fulihui.usercore.dal.dataobj.WechatUser;
import com.fulihui.usercore.dal.dataobj.WechatUserExample;
import com.fulihui.usercore.dal.mapper.WechatUserMapper;
import com.fulihui.usercore.dto.WechatUserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import java.util.List;

import static com.fulihui.usercore.biz.conv.WechatUserConv.*;

/**
 * @author Willard.Hu on 2016/5/21.
 */
@Repository
public class WechatUserRepositoryImpl implements WechatUserRepository {
    @Autowired
    private WechatUserMapper wechatUserMapper;

    @Override
    public void create(WechatUserDTO dto) {
        WechatUser record = toObj(dto);
        wechatUserMapper.insert(record);
    }

    @Override
    public boolean modify(WechatUserDTO dto) {
        WechatUser record = toObj(dto);
        return wechatUserMapper.updateByPrimaryKeySelective(record) > 0;
    }

    @Override
    public boolean updateDsiable(int disbale, String userId) {
        WechatUser record = new WechatUser();
        record.setDisable(disbale);
        WechatUserExample example = new WechatUserExample();
        example.createCriteria().andUserIdEqualTo(userId);
        wechatUserMapper.updateByExampleSelective(record, example);
        // 允许更新数量为0的情况
        return wechatUserMapper.updateByExampleSelective(record, example) >= 0;
    }

    @Override
    public List<WechatUserDTO> queryDsiabled() {
        WechatUserExample example = new WechatUserExample();
        example.createCriteria().andDisableEqualTo(1);
        List<WechatUser> list = wechatUserMapper.selectByExample(example);
        return toDTOList(list);
    }

    @Override
    public WechatUserDTO queryByPK(long id) {
        WechatUser obj = wechatUserMapper.selectByPrimaryKey(id);
        return toDTO(obj);
    }

    @Override
    public WechatUserDTO querySingle(String openId, String openIdType, String appid) {
        Assert.hasLength(openId);
        Assert.hasLength(openIdType);
        Assert.hasLength(appid);

        WechatUserExample example = new WechatUserExample();
        example.createCriteria().andOpenIdEqualTo(openId)
                .andOpenidTypeEqualTo(openIdType)
                .andAppidEqualTo(appid)
                .andDisableEqualTo(0);
        List<WechatUser> list = wechatUserMapper.selectByExample(example);
        // 要么空，要么只有1条
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return toDTO(list.get(0));
    }

    @Override
    public List<WechatUserDTO> queryByUnionid(String unionid, String appid) {
        WechatUserExample example = new WechatUserExample();
        example.createCriteria().andUnionidEqualTo(unionid)
                .andAppidEqualTo(appid)
                .andDisableEqualTo(0);
        List<WechatUser> list = wechatUserMapper.selectByExample(example);
        return toDTOList(list);
    }

    @Override
    public List<WechatUserDTO> queryByUserIds(List<String> userIds, String appid) {
        WechatUserExample example = new WechatUserExample();
        example.createCriteria().andUserIdIn(userIds)
                .andAppidEqualTo(appid)
                .andDisableEqualTo(0);
        List<WechatUser> list = wechatUserMapper.selectByExample(example);
        return toDTOList(list);
    }

    @Override
    public List<WechatUserDTO> query(WechatUserExample example) {
        return toDTOList(wechatUserMapper.selectByExample(example));
    }

    @Override
    public WechatUserDTO queryByUnionid(String unionid, String openIdType, String appid) {
        Assert.hasLength(unionid);
        Assert.hasLength(openIdType);
        Assert.hasLength(appid);

        WechatUserExample example = new WechatUserExample();
        WechatUserExample.Criteria criteria = example.createCriteria();
        criteria.andUnionidEqualTo(unionid)
                .andOpenidTypeEqualTo(openIdType)
                .andAppidEqualTo(appid)
                .andDisableEqualTo(0);
        List<WechatUser> list = wechatUserMapper.selectByExample(example);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return toDTO(list.get(0));
    }
}
